/*
 * Class Zoom //Not completed//
 */
function myZoom(){
	this.start = 0;
	this.stop = 0;
}
myZoom.prototype.getStart = function(){
	return this.start;
}
myZoom.prototype.getStop = function(){
	return this.stop;
}
myZoom.prototype.getLength = function(){
	return this.stop-(this.start-1);
}
myZoom.prototype.setStart = function(start){
	this.start = start;
}
myZoom.prototype.setStop = function(stop){
	this.stop = stop;
}


/*
 * Class Slider
 */


function mySlider(tagId, parentTagId){
	// Slider has a range of 200 pixels
	this.tagId = tagId;
	this.parentTagId = parentTagId;
	
	this.range = 200;
	
	// No ticks for this example
	this.tickSize = 0;
	
	// We'll set a minimum distance the thumbs can be from one another
	this.minThumbDistance = 0;
	
	this.thumbWidth = 20;
	
	// Initial values for the thumbs
	//this.initValues = [0, 200];
	
	this.seqStart = 1;
	this.seqStop = 1000;
	
	this.slider = new Object();
	
	// Conversion factor from 0-200 pixels to 100-1000
	// Note 20 pixels are subtracted from the range to account for the
	// thumb values calculated from their center point (10 pixels from
	// the center of the left thumb + 10 pixels from the center of the
	// right thumb)
	
	//var cf = 1000/(range - 20);
	//this.cf = (this.seqStop - this.seqStart) / (this.range);
	
}

mySlider.prototype.setHtml = function(){
	this.html = "<div id='" + this.tagId + "_bg' class='yui-h-slider'>";
	this.html += "<div id='" + this.tagId + "_min_thumb' class='yui-slider-thumb'><img src='img/left-thumb2.png'></div>";
	this.html += "<div id='" + this.tagId + "_max_thumb' class='yui-slider-thumb'><img src='img/right-thumb2.png'></div>";
	this.html += "</div>";
	this.html += "<label>Chromosome Start: <input type='text' id='" + this.tagId + "_from' size='15' maxlength='15' value=''></label> ";
	this.html += "<label>Chromosome End: <input type='text' id='" + this.tagId + "_to' size='15' maxlength='15' value=''></label>";
	this.html += "<button id='" + this.tagId + "_btn'>Update Slider</button>";
	this.html += "<button id='" + this.tagId + "_zoom'>Zoom</button>";
	var parentContainer = document.getElementById(this.parentTagId);
	//new YAHOO.util.Element(this.parentTagId);
	parentContainer.innerHTML = this.html;

}

mySlider.prototype.setRange = function(range){
	this.range = range;
}

mySlider.prototype.deletePxSuffix = function(value){
	var newValue = value;
	var value = value.toString();
	if(value.indexOf("px") != -1){
		var newValue_temp = value.split("px");
		newValue = newValue_temp[0];
	};
	//if (newValue=="NaN"){
		//alert("Error on deletePxSuffix. The input for this function is not valid");
	//}
	var newValueTemp = newValue +"";
	if(newValueTemp == "NaN" || newValueTemp == "auto"){
		newValue = 0;
	}
	return parseFloat(newValue);
}

mySlider.prototype.setBgImg = function(imgFileName, pxWith){
	var sliderBg = new YAHOO.util.Element(this.tagId+"_bg");
	sliderBg.setStyle('width', pxWith);
	sliderBg.setStyle('background-image', 'url('+ imgFileName +')');
	sliderBg.setStyle('background-repeat', 'repeat-x');
	var sliderBgLeftSuffix = sliderBg.getStyle('left');
	var sliderBgLeft = this.deletePxSuffix(sliderBgLeftSuffix);
	var leftValue = (this.thumbWidth/2)+sliderBgLeft;
	sliderBg.setStyle('left', leftValue+'px');
	childrenNumber = sliderBg.get('childNodes').length;
	for (var j = 0; j < sliderBg.get('childNodes').length; j++) {
		if(sliderBg.get('childNodes')[j].id){
			var sliderBgChild = new YAHOO.util.Element(sliderBg.get('childNodes')[j]);
			var sliderBgChildLeft = this.deletePxSuffix(sliderBgChild.getStyle('left'));
			sliderBgChild.setStyle('left', '-' + ((this.thumbWidth/2)+sliderBgChildLeft) +'px');
		}
	}
}

mySlider.prototype.setSeqStart = function(seqStart){
	this.seqStart = seqStart;
}

mySlider.prototype.setSeqStop = function(seqStop){
	this.seqStop = seqStop;
}




mySlider.prototype.setSlider = function(){
}
	

mySlider.prototype.getPxSelection = function(){
	//return this.range = range;
}

mySlider.prototype.getSeqSelection = function(){
	return this.range;
}

mySlider.prototype.setSlider = function(){
		var cf = (this.seqStop - this.seqStart) / (this.range);
		var seqStart = this.seqStart;
		var seqStop = this.seqStop;
		var range = this.range;
		var minThumbDistance = this.minThumbDistance;
		
		var convert = function(val){
			//return Math.round(val * cf);
			return Math.round(val * cf + seqStart);
		}
		var Dom = YAHOO.util.Dom;
        var bg = Dom.get(this.tagId + "_bg"),
            info    = Dom.get(this.tagId + "_info"),
            from    = Dom.get(this.tagId + "_from"),
            to      = Dom.get(this.tagId + "_to");

        // Create the DualSlider
        this.slider = YAHOO.widget.Slider.getHorizDualSlider(bg,
            this.tagId+"_min_thumb", this.tagId+"_max_thumb",
            this.range, this.tickSize, [0,this.range]);

        this.slider.minRange = this.minThumbDistance;
		
        
		var slider = this.slider;
        // Custom function to update the text fields, the converted value
        // report and the slider's title attribute
        var updateUI = function () {
            //from.value = slider.minVal;
            //to.value   = slider.maxVal;
			//from.value = convert(slider.minVal);
			//to.value = convert(slider.maxVal);

			from.value = convert(slider.minVal);
			to.value = convert(slider.maxVal);
			

            // Update the converted values and the slider's title.
            // Account for the thumb width offsetting the value range by
            // subtracting the thumb width from the max value.
            var min = convert(slider.minVal),
                max = convert(slider.maxVal);

            //info.innerHTML = "MIN: <strong>" + min + "</strong><br>" +
            //                "MAX: <strong>" + max + "</strong>";
            //bg.title  = "Current range " + min + " - " + max;
        };
		
		

        // Subscribe to the dual thumb slider's change and ready events to
        // report the state.
        this.slider.subscribe('ready', updateUI);
        this.slider.subscribe('change', updateUI);

		//UPDATE SLIDER
		var updateSlider = function () {
            // Get the int values from the inputs
			var min = Math.abs(parseInt((from.value-seqStart+1)/cf,10)|0),
                max = Math.abs(parseInt((to.value-seqStart+1)/cf,10)|0);
            if (min > max) {
                var hold = min;
                min = max;
                max = hold;
            }
            // Verify the values are in range
            min = Math.min(min,range - 30);
            max = Math.max(Math.min(max,range),min + 20 + minThumbDistance);
            // Set the new values on the slider
            slider.setValues(min,max);
		}
		
		//ZOOM REGION
		var zoomRegion = function(){
			var updatedFromValue = parseInt(from.value)
			if (parseInt(from.value) < seqStart) {
				updatedFromValue = seqStart;
				from.value = seqStart;
			}
			var updatedToValue = parseInt(to.value)
			if (parseInt(to.value) > seqStop) {
				updatedToValue = seqStop;
				to.value = seqStop;
			}
			//Insert code to execute Zoom
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.redrawZoom(
				YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome,
				updatedFromValue,
				updatedToValue
			);
		}
		
        // Wire up the button to update the slider
        YAHOO.util.Event.on(this.tagId + '_btn','click',function () {
			updateSlider();
        });
		
		YAHOO.util.Event.on(this.tagId + '_zoom','click',function () {
			zoomRegion();
			updateSlider();
        });
        // Attach the slider to the YAHOO.example namespace for public probing
        //this.slider = slider;
    };

